﻿//-------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="Russell Wilkins">
//     Copyright (c) Russell Wilkins 2014. All Rights Reserved.
// </copyright>
// <author>Russell Wilkins</author>
// <license href="license.txt">
//     The use of this software is governed by the Microsoft Public License
//     which is included with this distribution.
// </license>
//-------------------------------------------------------------------------------------------
namespace Ripple.TestClient.ViewModels
{
    using Ripple.TestClient.Views;
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;

    /// <summary>
    /// TODO: Describe the class
    /// </summary>
    public class MainViewModel : CloseableViewModel<INotifyPropertyChanged>
    {
        public static readonly DependencyProperty AccountIdProperty = DependencyProperty.Register("AccountId", typeof(string), typeof(MainViewModel), new FrameworkPropertyMetadata(string.Empty, new PropertyChangedCallback(AccountIdChangedCallBack)));
        public static readonly DependencyProperty AccountViewModelProperty = DependencyProperty.Register("AccountViewModel", typeof(AccountViewModel), typeof(MainViewModel), new PropertyMetadata(null));
        public static readonly DependencyProperty ActionBarViewModelProperty = DependencyProperty.Register("ActionBarViewModel", typeof(ActionBarViewModel), typeof(MainViewModel));

        private ReadOnlyCollection<MenuItemViewModel> menuItems;

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IRippleContext rippleContext)
        {
            this.RippleContext = rippleContext;
            this.DisplayName = "Ripple.Net Test Client";
            this.ActionBarViewModel = new ActionBarViewModel(this.RippleContext);
            this.ActionBarViewModel.AccountSelected += ActionBarViewModel_AccountSelected;
            this.ActionBarViewModel.AccountId = Settings.Default.AccountId;
        }

        public string AccountId
        {
            get { return (string)GetValue(AccountIdProperty); }
            set { SetValue(AccountIdProperty, value); }
        }

        public AccountViewModel AccountViewModel
        {
            get { return (AccountViewModel)GetValue(AccountViewModelProperty); }
            set { SetValue(AccountViewModelProperty, value); }
        }

        public ActionBarViewModel ActionBarViewModel
        {
            get { return (ActionBarViewModel)GetValue(ActionBarViewModelProperty); }
            set { SetValue(ActionBarViewModelProperty, value); }
        }

        public ReadOnlyCollection<MenuItemViewModel> MenuItems
        {
            get
            {
                if (this.menuItems == null)
                {
                    this.menuItems = new ReadOnlyCollection<MenuItemViewModel>(this.CreateMenuItems());
                }

                return this.menuItems;
            }
        }

        public IRippleContext RippleContext { get; private set; }

        private static void AccountIdChangedCallBack(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as MainViewModel).UpdateAccount(args.NewValue as string);
        }

        private void ActionBarViewModel_AccountSelected(object sender, EventArgs e)
        {
            this.UpdateAccount(this.ActionBarViewModel.AccountId);
        }

        private List<MenuItemViewModel> CreateMenuItems()
        {
            MenuItemViewModel fileMenu = new MenuItemViewModel();
            fileMenu.DisplayName = "_FILE";

            MenuItemViewModel exitMenuItem = new MenuItemViewModel();
            exitMenuItem.DisplayName = "E_xit";
            exitMenuItem.InputGestureText = "Alt+F4";
            exitMenuItem.Command = this.CloseCommand;
            fileMenu.ListItems.Add(exitMenuItem);

            MenuItemViewModel helpMenu = new MenuItemViewModel();
            helpMenu.DisplayName = "_HELP";

            var list = new List<MenuItemViewModel>();
            list.Add(fileMenu);
            list.Add(helpMenu);

            return list;
        }

        private void UpdateAccount(string accountId)
        {
            this.AccountViewModel = new AccountViewModel(this.RippleContext);
            this.AccountViewModel.SetAccount(accountId);
            Settings.Default.AccountId = accountId;
        }
    }
}